﻿using Library.Core.Domain;
using Library.Core.Domain.Specification;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Domain.Specifications.Authority.Menu
{
    public class CreateMenuPermissionSpecification : IOperateSpecification<MenuPermissionInfoEntity>
    {
        private readonly IMenuPermissionRepository _menuPermissionRepository;

        public CreateMenuPermissionSpecification(IMenuPermissionRepository menuPermissionRepository)
        {
            _menuPermissionRepository = menuPermissionRepository;
        }

        public async Task<bool> SatisfiedBy(MenuPermissionInfoEntity entity)
        {
            if (await _menuPermissionRepository.AnyAsync(x => (x.Name.Equals(entity.Name) || x.Code.Equals(entity.Code)) && x.MenuId == entity.MenuId))
            {
                throw new DomainException("同菜单级下,操作权限名称或代码不能重复");
            }
            if (await _menuPermissionRepository.AnyAsync(x => x.Code.Equals(entity.Code)))
            {
                throw new DomainException("操作权限代码全局唯一，不能重复");
            }
            return true;
        }
    }
}
